package com.example.gaoyang.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.gaoyang.annotation.NoAuth;
import com.example.gaoyang.domain.request.SysUserRequest;
import com.example.gaoyang.domain.vo.SysElementVO;
import com.example.gaoyang.domain.vo.SysUserVO;
import com.example.gaoyang.request.Result;
import com.example.gaoyang.service.SysUserService;
import com.example.gaoyang.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @PostMapping("/addUser")
    @NoAuth
    public Result addUser(@RequestBody SysUserRequest sysUserRequest) {
        sysUserService.addUser(sysUserRequest);
        return ResultUtil.success(String.format("用户创建成功,用户名:[%s]", sysUserRequest.getUsername()));
    }

    @PostMapping("/login")
    @NoAuth
    public Result<SysUserVO> login(@RequestBody SysUserRequest sysUserRequest) {
        return ResultUtil.success(sysUserService.login(sysUserRequest));
    }

    @PostMapping("/logout")
    @NoAuth
    public Result logout(@RequestBody SysUserRequest sysUserRequest) {
        sysUserService.logout(sysUserRequest);
        return ResultUtil.success(String.format("用户[%s]退出成功", sysUserRequest.getUsername()));
    }

    @PostMapping("/getUserInfo")
    public Result<SysUserVO> getUserInfo(@RequestBody SysUserRequest sysUserRequest) {
        return ResultUtil.success(sysUserService.getUserInfo(sysUserRequest));
    }

    @PostMapping("/getUserList")
    public Result<List<SysUserVO>> getUserList(@RequestBody SysUserRequest sysUserRequest) {
        return ResultUtil.success(sysUserService.getUserList(sysUserRequest));
    }

    @PostMapping("/removeUser")
    public Result removeUser(@RequestBody SysUserRequest sysUserRequest) {
        sysUserService.removeUser(sysUserRequest);
        return ResultUtil.success(String.format("用户删除成功,用户名:[%s]", sysUserRequest.getUsername()));
    }

    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody SysUserRequest sysUserRequest) {
        sysUserService.updateUser(sysUserRequest);
        return ResultUtil.success(String.format("用户更新成功,用户id:[%s]", sysUserRequest.getId()));
    }

    @PostMapping("/getUserPage")
    public Result getUserPage(Page page, @RequestBody SysUserRequest sysUserRequest) {
        return ResultUtil.success(sysUserService.getUserPage(page, sysUserRequest));
    }

    @PostMapping("/getSysElementByUser")
    public Result<List<SysElementVO>> getSysElementByUser(@RequestBody SysUserRequest sysUserRequest) {
        return ResultUtil.success(sysUserService.getSysElementByUser(sysUserRequest));
    }

    @PostMapping("/getButtonByUser")
    public Result<List<SysElementVO>> getButtonByUser(@RequestBody SysUserRequest sysUserRequest) {
        return ResultUtil.success(sysUserService.getButtonByUser(sysUserRequest));
    }

    @PostMapping("/resetPassword")
    public Result resetPassword(@RequestBody SysUserRequest sysUserRequest) {
        sysUserService.resetPassword(sysUserRequest);
        return ResultUtil.success(String.format("密码重置成功,用户名:[%s]", sysUserRequest.getUsername()));
    }
}
